package com.wchen.gdoc.generate.mapping.resolver;


import com.wchen.gdoc.generate.config.ApplicationConfigProvide;
import com.wchen.gdoc.generate.mapping.data.ParamData;
import com.wchen.gdoc.generate.producer.ParamValueProducer;
import com.wchen.gdoc.generate.resource.structure.ObjectStructure;
import com.wchen.gdoc.generate.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Map;

/**
 * 抽象对象结构解析
 *
 * @author  wchen
 * @date    2019/11/7
 */
public abstract class AbsObjectStructureResolver implements TypeStructureResolver {
    public final static Logger LOGGER = LoggerFactory.getLogger(AbsObjectStructureResolver.class);
    public final static Boolean ENABLE_DEBUG = ApplicationConfigProvide.enableDebug();
    private final static Map<String, String> TYPE_MAPPING = ApplicationConfigProvide.getApplicationConfig().getTypeMapping();


    public AbsObjectStructureResolver() {
    }

    /**
     * 获取测试值
     *
     * @param classType
     * @return
     */
    public Object getParamValue(String classType, String paramName) {
        ParamValueProducer paramValueProducer = ApplicationConfigProvide.getApplicationConfig().getParamValueProducer();
        return paramValueProducer.createParamValue(classType, paramName);
    }


    /**
     * 生成参数实体
     *
     * @param paramName
     * @param classType
     * @param paramGenericType
     * @param paramObjValue
     * @param commentText
     * @return
     */
    public ParamData createParamData(String paramName, String classType, String paramGenericType, Object paramObjValue, String commentText, Boolean hasChild) {
        ParamData paramData = new ParamData();
        paramData.setDataType(getTypeMapping(classType, paramGenericType));
        paramData.setClassType(classType);
        paramData.setParamName(paramName);
        paramData.setParamValue(paramObjValue);
        paramData.setGenericType(paramGenericType);
        paramData.setFieldCommentText(commentText);
        paramData.setHasChild(hasChild);
        return paramData;
    }

    /**
     * 调用解析器链
     *
     * @param objectStructure
     * @return
     */
    public ParamData doResolverChain(ObjectStructure objectStructure) {
        for (TypeStructureResolver typeStructureResolver : ApplicationConfigProvide.getApplicationConfig().getTypeStructureResolverChain()) {
            if (typeStructureResolver.isSupport(objectStructure.getType())) {
                return typeStructureResolver.doResolve(objectStructure);
            }
        }
        return new ParamData();
    }

    /**
     * 获取映射类型
     * @param type
     * @param genericType
     * @return
     */
    private String getTypeMapping(String type, String genericType) {
        type = type.substring(type.lastIndexOf(".") + 1, type.length());
        if(StringUtil.isNotBlank(genericType)){
            genericType = genericType.substring(genericType.lastIndexOf(".") + 1, genericType.length());
            type = type + "<" + genericType + ">";
            String mappingResult = TYPE_MAPPING.get(type);
            if(StringUtil.isNotBlank(mappingResult)) {
                return mappingResult;
            }
            return TYPE_MAPPING.get("List<Object>");
        }

        String mappingResult = TYPE_MAPPING.get(type);
        if(StringUtil.isNotBlank(mappingResult)) {
            return mappingResult;
        }
        return TYPE_MAPPING.get("Object");
    }
}